from flask import Blueprint,request,jsonify
from conf.comm import sqlalchemy_context
from database import *
import json,re
from werkzeug.security import generate_password_hash,check_password_hash
#蓝图初始化
bp_users = Blueprint('users',__name__)
#注册功能实现
@bp_users.route('/reg/',methods=['POST'])
@sqlalchemy_context()
def reg():
    #获取vue信息
    data = request.data
    if not data:
       data  = request.form
    else:
       data = json.loads(request.data)

    #正则验证
    res = re.match('^1[3-9][0-9]{9}$',data['mobile'])
    if not res:
        return jsonify({'code':11111,'msg':'手机号不合法'})

    #向数据库提交数据
    try:
        user = User(username=data['username'],password=generate_password_hash(data['password']),mobile=data['mobile'])
        db.session.add(user)
        db.session.commit()
        return jsonify({'code':200})
    except:
        return jsonify({'code':20020})
    
